package com.sdhs.projectschedule.service.impl;

import com.sdhs.common.utils.DateUtils;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.listmanage.domain.entity.GcManageList;
import com.sdhs.listmanage.mapper.GcManageListMapper;
import com.sdhs.projectschedule.domain.GcProgressIntegratedConstruction;
import com.sdhs.projectschedule.domain.GcProgressSupplyFill;
import com.sdhs.projectschedule.domain.gcProgressIntegratedConstructionFill;
import com.sdhs.projectschedule.mapper.GcProgressIntegratedConstructionMapper;
import com.sdhs.projectschedule.mapper.gcProgressIntegratedConstructionFillMapper;
import com.sdhs.projectschedule.service.IGcProgressIntegratedConstructionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 集成施工进度Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-02
 */
@Service
public class GcProgressIntegratedConstructionServiceImpl implements IGcProgressIntegratedConstructionService {
    @Autowired
    private GcProgressIntegratedConstructionMapper gcProgressIntegratedConstructionMapper;

    @Autowired
    private gcProgressIntegratedConstructionFillMapper gcIntegratedConstructionFillMapper;

    @Autowired
    private GcManageListMapper gcManageListMapper;
    /**
     * 查询集成施工进度
     *
     * @param id 集成施工进度主键
     * @return 集成施工进度
     */
    @Override
    public GcProgressIntegratedConstruction selectGcProgressIntegratedConstructionById(Long id) {
        return gcProgressIntegratedConstructionMapper.selectGcProgressIntegratedConstructionById(id);
    }

    /**
     * 查询集成施工进度列表
     *
     * @param gcProgressIntegratedConstruction 集成施工进度
     * @return 集成施工进度
     */
    @Override
    public List<GcProgressIntegratedConstruction> selectGcProgressIntegratedConstructionList(GcProgressIntegratedConstruction gcProgressIntegratedConstruction) {
        return gcProgressIntegratedConstructionMapper.selectGcProgressIntegratedConstructionList(gcProgressIntegratedConstruction);
    }
    /**
     * 查询集成施工进度列表-子表清单数据
     *
     * @return 集成施工进度-子表清单数据
     */
    @Override
    public List<gcProgressIntegratedConstructionFill> selectgcProgressIntegratedConstructionFillList(gcProgressIntegratedConstructionFill gcIntegratedConstructionFill) {
        return gcIntegratedConstructionFillMapper.selectgcProgressIntegratedConstructionFillList(gcIntegratedConstructionFill);
    }

    /**
     * 新增集成施工进度
     *
     * @param gcProgressIntegratedConstruction 集成施工进度
     * @return 结果
     *
     */
    @Override
    public int insertGcProgressIntegratedConstruction(GcProgressIntegratedConstruction gcProgressIntegratedConstruction) {
        if (gcProgressIntegratedConstruction.getId() == null) {
            gcProgressIntegratedConstruction.setCreateBy(SecurityUtils.getUsername());
            gcProgressIntegratedConstruction.setCreateTime(DateUtils.getNowDate());
            //新增集成施工主表信息
            gcProgressIntegratedConstruction.setConstructionSchedule(handleScale(gcProgressIntegratedConstruction.getSupplyFillList()));
            gcProgressIntegratedConstructionMapper.insertGcProgressIntegratedConstruction(gcProgressIntegratedConstruction);
        }
        //新增集成施工子表信息
        gcProgressIntegratedConstruction.getSupplyFillList().forEach(progressSupplyFill -> {
            progressSupplyFill.setSupplyId(gcProgressIntegratedConstruction.getId());
            progressSupplyFill.setCreateBy(SecurityUtils.getUsername());
            progressSupplyFill.setCreateTime(DateUtils.getNowDate());
        });
        return gcIntegratedConstructionFillMapper.insertGcProgressIntegratedConstructionFillList(gcProgressIntegratedConstruction.getSupplyFillList());
    }

    /**
     * 修改集成施工进度
     *
     * @param gcProgressIntegratedConstruction 集成施工进度
     * @return 结果
     */
    @Override
    public int updateGcProgressIntegratedConstruction(GcProgressIntegratedConstruction gcProgressIntegratedConstruction) {
        gcProgressIntegratedConstruction.setUpdateBy(SecurityUtils.getUsername());
        gcProgressIntegratedConstruction.setUpdateTime(DateUtils.getNowDate());
        return gcProgressIntegratedConstructionMapper.updateGcProgressIntegratedConstruction(gcProgressIntegratedConstruction);
    }

    /**
     * 批量删除集成施工进度
     *
     * @param ids 需要删除的集成施工进度主键
     * @return 结果
     */
    @Override
    public int deleteGcProgressIntegratedConstructionByIds(Long[] ids) {
        return gcProgressIntegratedConstructionMapper.deleteGcProgressIntegratedConstructionByIds(ids);
    }

    /**
     * 删除集成施工进度信息
     *
     * @param id 集成施工进度主键
     * @return 结果
     */
    @Override
    public int deleteGcProgressIntegratedConstructionById(Long id) {
        return gcProgressIntegratedConstructionMapper.deleteGcProgressIntegratedConstructionById(id);
    }

    /*计算集成施工进度比例*/
    private String handleScale(List<gcProgressIntegratedConstructionFill> supplyFillList) {
        /*判断是否为空然后返回0*/
        if (supplyFillList == null || supplyFillList.isEmpty()) {
            return "0";
        }
        //获取所有的清单用于计算比例
        List<GcManageList> gcManageLists = gcManageListMapper.selectGcManageListByProject(supplyFillList.get(0).getProjectId());
        if (gcManageLists == null || gcManageLists.isEmpty()) {
            return "0";
        }
        /*去掉quantity是0或者是空的的子目号*/
        List<GcManageList> filteredManageLists = gcManageLists.stream()
                .filter(item -> {
                    try {
                        double quantity = Double.parseDouble(String.valueOf(item.getQuantity()));
                        return quantity != 0;
                    } catch (NumberFormatException e) {
                        return false;
                    }
                })
                .collect(Collectors.toList());

        double ratioSum = 0;
        for (gcProgressIntegratedConstructionFill item : supplyFillList) {
            try {
                double supplyQuantity = Double.parseDouble(String.valueOf(item.getSupplyQuantity()));
                double quantity = Double.parseDouble(String.valueOf(item.getQuantity()));
                if (quantity != 0) {
                    ratioSum += supplyQuantity / quantity;
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
                continue;
            }
        }

        double averageRatio = ratioSum / filteredManageLists.size();
        /*不保留两位小数
        return String.valueOf(averageRatio * 100);*/
        double percentage = averageRatio * 100;
        if(percentage > 100){
            percentage = 100;
        }

        // 使用 DecimalFormat 保留两位小数
        DecimalFormat df = new DecimalFormat("#.##");
        return df.format(percentage); // 例如：85.67
    }
}
